﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.ProjectContract;
using Bricks.Business.Models.Account;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.AlterManagement;
using Bricks.Business.Data.Repositories.ProjectContract;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IProjectInfoService))]
    internal class ProjectInfoService : IProjectInfoService
    {
        [Import]
        protected IProjectInfoRepository ProjectInfoRepository { get; set; }

        public List<ProjectInfoView> GetProjectInfoList()
        {
            List<ProjectInfoView> projectInfoViewList = new List<ProjectInfoView>();
            foreach (ProjectInfo projectInfo in ProjectInfoRepository.Entities)
            {
                projectInfoViewList.Add(new ProjectInfoView()
                {
                    ID = projectInfo.ID,
                    ProjectName = projectInfo.ProjectName,
                    OrderNo = projectInfo.OrderNo
                });
            }
            return projectInfoViewList.OrderBy(p => p.OrderNo).ToList();
        }

        public IQueryable<ProjectInfo> ProjectInfoes
        {
            get { return ProjectInfoRepository.Entities; }
        }


        public OperationResult Add(ProjectInfo model)
        {
            ProjectInfo record = model.CopyTo<ProjectInfo>();
            //record.ID = GuidHelper.NewGuid();
            record.OrderNo = ProjectInfoRepository.GetNextOrderNo(m => m.OrderNo);  //获取下一个排序号
            ProjectInfoRepository.Insert(record);
            return new OperationResult(OperationResultType.Success, "新增成功", record);
        }

        public OperationResult Modify(ProjectInfo model)
        {
            PublicHelper.CheckArgument(model, "model");
            ProjectInfo record = ProjectInfoes.FirstOrDefault(m => m.ID == model.ID);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            record = model.CopyTo<ProjectInfo>();
            if (ProjectInfoRepository.Update(record) >= 0)
                return new OperationResult(OperationResultType.Success, "修改成功", record);
            return new OperationResult(OperationResultType.Error, "保存失败");
        }

        public OperationResult Delete(Guid id)
        {
            ProjectInfo record = ProjectInfoes.FirstOrDefault(m => m.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (ProjectInfoRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }

        public OperationResult SetOrder(Guid id, string upOrDown)
        {
            IQueryable<ProjectInfo> entities = ProjectInfoRepository.Entities;  //找出可能涉及排序的实体集合（同一PID下）
            var exeResult = ProjectInfoRepository.SetOrder(entities, id, "OrderNo", upOrDown);  //执行向上向下操作
            return exeResult;
        }
    }
}